package icu.groot.alan.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import icu.groot.alan.dao.AnnounceDao;
import icu.groot.alan.dao.DepartmentDao;
import icu.groot.alan.dao.ReportDao;
import icu.groot.alan.dao.impl.AnnounceDaoImpl;
import icu.groot.alan.dao.impl.DepartmentDaoImpl;
import icu.groot.alan.dao.impl.ReportDaoImpl;
import icu.groot.alan.entity.Department;
import icu.groot.alan.entity.Report;
import icu.groot.alan.entity.ReportExport;
import icu.groot.alan.service.ReportService;
import icu.groot.alan.util.Dao;
import icu.groot.alan.util.R;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ReportServiceImpl implements ReportService {
    ReportDao reportDao = new ReportDaoImpl();
    DepartmentDao departmentDao = new DepartmentDaoImpl();
    AnnounceDao announceDao = new AnnounceDaoImpl();
    @Override
    public R add(int userCode, int announceId, double temperature, String oneselfHealth, String oneselfHealthDetails, String route, String address, String familyHealth, String familyHealthDetails, String anomaly) {
        Connection connection = Dao.getConnection();
        if (temperature < 36.0 || temperature > 37.0) anomaly = "异常";
        try {
            int updateRow = reportDao.add(connection, userCode, announceId, temperature, oneselfHealth, oneselfHealthDetails, route, address, familyHealth, familyHealthDetails, anomaly, System.currentTimeMillis() / 1000);
            connection.commit();
            if (updateRow <= 0) throw new RuntimeException("添加数据返回行数小于等于0");
        } catch (SQLException e) {
            try {
                connection.rollback();
            } catch (SQLException ex) {
                e.printStackTrace();
            }
            e.printStackTrace();
            Dao.closeResource(connection);
            return R.error("系统异常");
        }
        Dao.closeResource(connection);
        return R.ok("已完成");
    }

    @Override
    public int checkAdd(int userCode, int announceId) {
        Connection connection = Dao.getConnection();
        int count = 0;
        try {
            count = reportDao.checkAdd(connection, userCode, announceId);
        } catch (SQLException e) {
            e.printStackTrace();
            Dao.closeResource(connection);
            return count;
        }
        Dao.closeResource(connection);
        return count;
    }

    @Override
    public Map<String, List<ReportExport>> reportExcel(int announceId) {
        Connection connection = Dao.getConnection();
        Map<String, List<ReportExport>> reportExportMap = new HashMap<>();
        try {
            List<Report> reportsByAnnounceId = reportDao.getReportByAnnounceId(connection, announceId);
            ReportExport reportExport = null;
            StringBuilder conditionBuffer =null;
            List<ReportExport> reportExports = null;
            for (Report report : reportsByAnnounceId) {
                conditionBuffer =  new StringBuilder();
                if (report.getAnomaly() == null){
                    conditionBuffer.append("未上报");
                } else if (report.getAnomaly().equals("正常")) {
                    conditionBuffer.append(report.getAnomaly());
                } else if(report.getAnomaly().equals("异常")){
                    conditionBuffer.append("异常状况\r\n");
                    if (!report.getOneselfHealth().equals("健康")){
                        conditionBuffer.append("自身情况: ").append(report.getOneselfHealthDetails()).append(";\r\n ");
                    }
                    double temperature = Double.parseDouble(report.getTemperature());
                    if (temperature > 37.0) {
                        conditionBuffer.append("体温: 体温偏高;\r\n ");
                    }
                    if (temperature < 36.0) {
                        conditionBuffer.append("体温: 体温偏低;\r\n ");
                    }
                    if (!report.getFamilyHealth().equals("健康")){
                        conditionBuffer.append("家庭情况: ").append(report.getFamilyHealthDetails()).append(";\r\n ");
                    }
                    conditionBuffer.append("住址: ").append(report.getAddress()).append(";\r\n ");
                    conditionBuffer.append("近期路线: ").append(report.getRouter()).append(";\r\n ");
                }
                reportExport = new ReportExport(report.getUserCode() + "", report.getUserName(), conditionBuffer.toString());
                if (!reportExportMap.containsKey(report.getDepartment())){
                    reportExports = new ArrayList<>();
                    reportExports.add(reportExport);
                    reportExportMap.put(report.getDepartment(),reportExports);
                    continue;
                }
                reportExportMap.get(report.getDepartment()).add(reportExport);
            }
            Dao.closeResource(connection);
            return reportExportMap;
        } catch (SQLException e) {
            e.printStackTrace();
            return  reportExportMap;
        }
    }
}
